<?php

/**
 * PercentBar class
 *
 * Plot PercentBar
 *
 * @name PercentBar
 * @author DiMoN
 * @copyright Dmitry Pavlov
 */

class PercentBar extends Chart implements IChart
{
    const CHART_ID = "PercentBar",
    CHART_TYPE = 3,
    CHART_NAME = "Процентная столбчатая диаграмма";

    /**
     * PercentBar constructor
     *
     */
    function __construct()
    {
        $this->isParamsValid();
    }

    /**
     * Gets chart ID specified by the const
     *
     * @return string
     */
    public static function getChartID()
    {
        return self::CHART_ID;
    }

    /**
     * Gets chart Type specified by the const
     *
     * @return int
     */
    public static function getChartType()
    {
        return self::CHART_TYPE;
    }

    /**
     * Gets chart Name specified by the const
     *
     * @return string
     */
    public static function getChartName()
    {
        return self::CHART_NAME;
    }

    /**
     * Checks the validity of the data arrays
     *
     */
    public function isParamsValid()
    {
        if (!isset(parent::$Data3D))
        {
            throw new Exception(self::CHART_ID . ": Data3D array is not set");
        }

        if (!isset(parent::$Labels))
        {
            throw new Exception(self::CHART_ID . ": Labels array is not set");
        }

        if (!isset(parent::$Legend))
        {
            throw new Exception(self::CHART_ID . ": Legend array is not set");
        }
    }

    /**
     * Plots chart
     *
     * @return string
     */
    public function plot()
    {
        $c = new XYChart(parent::$Width, parent::$Height, parent::$Background, 0, 1);
        $c->setRoundedFrame();

        $c->setPlotArea(
        parent::$Width  / 7,
        parent::$Height / 10,
        parent::$Width  - parent::$Width  / 2.7,
        parent::$Height - parent::$Height / 5 - parent::$LabelLevel * 10,
        $c->linearGradientColor(
        parent::$Width  - parent::$Width  / 7.3,
        parent::$Height - parent::$Height / 1,
        parent::$Width  - parent::$Width  / 7.3,
        parent::$Height - parent::$Height / 8,
        parent::$BackgroundArea1,
        parent::$BackgroundArea2),
        -1,
        parent::$BorderColor,
        parent::$HorizontalLineColor,
        parent::$VerticalLineColor
        );

        $legendBox = $c->addLegend(parent::$Width - parent::$Width / 4.7, parent::$Height / 11.5, true, '', 10);
        $legendBox->setBackground(Transparent, Transparent);
        $legendBox->setKeySize(16, 32);

        $textBoxObj = $c->addTitle(parent::$Title, parent::$TitleFont, parent::$TitleFontSize, parent::$ColorTitle);
        $textBoxObj->setBackground(parent::$BackgroundTitle1, parent::$BackgroundTitle2, glassEffect());

        /**
         * Adding labels
         */
        $textBoxObj = $c->xAxis->setLabels(parent::$LeveledLabels);

        $c->xAxis->setTitle(parent::$NameY, parent::$LabelFont);
        $textBoxObj->setFontAngle(parent::$LabelFontAngle);
        $layer = $c->addBarLayer2(Percentage);

        /**
         * Adding data and legend
         */
        foreach (parent::$Data3D as $key => $value)
        {
            $layer->addDataSet($value, parent::getColor(), parent::$Legend[$key]);
        }

        $layer->setBorderColor(Transparent);
        $textBoxObj = $layer->setDataLabelStyle();
        $textBoxObj->setAlignment(Center);
        $layer->setLegend(ReverseLegend);

        return parent::saveChart(&$c);
    }
}

?>